#ifndef CLICKABLEPIXMAPITEM_H
#define CLICKABLEPIXMAPITEM_H

#include <QObject>

class ClickablePixmapItem : public QGraphicsItem
{
    Q_OBJECT
    public:
    // 构造函数：传入正常状态和悬停状态的图片
    ClickablePixmapItem(const QPixmap& normal, const QPixmap& hover, QGraphicsItem* parent = nullptr)
        : QGraphicsPixmapItem(normal, parent), 
        m_normal(normal), 
        m_hover(hover) 
    {
        setAcceptHoverEvents(true); // 启用悬停事件
    }
    
signals:
    void clicked(); // 点击信号
    
protected:
    // 鼠标悬停进入时切换为悬停图片
    void hoverEnterEvent(QGraphicsSceneHoverEvent*) override {
        setPixmap(m_hover);
    }
    
    // 鼠标悬停离开时恢复正常图片
    void hoverLeaveEvent(QGraphicsSceneHoverEvent*) override {
        setPixmap(m_normal);
    }
    
    // 鼠标点击事件
    void mousePressEvent(QGraphicsSceneMouseEvent* event) override {
        if (event->button() == Qt::LeftButton) {
            emit clicked(); // 触发点击信号
        }
        QGraphicsPixmapItem::mousePressEvent(event);
    }
    
private:
    QPixmap m_normal; // 正常状态图片
    QPixmap m_hover;  // 悬停状态图片
};

#endif // CLICKABLEPIXMAPITEM_H
